import pymysql

class Utility:
    # 创建数据库连接对象
    def __init__(self):
        self.db = pymysql.connect("localhost","root","","woniuATM",charset="utf8")
        self.cur = self.db.cursor()

    # 对数据库的增、删、改
    def db_operation(self,sql):
        self.cur.execute(sql)
        self.db.commit()

    # 对数据库的查询(一条数据）
    def query_one(self,sql):
        self.cur.execute(sql)
        self.db.commit()
        content = self.cur.fetchone()
        return content

    # 对数据库的查询(所有数据）
    def query_all(self,sql):
        self.cur.execute(sql)
        self.db.commit()
        content = self.cur.fetchall()
        return content

    # 判断一个字符串能否被转换为一个有效的数字
    def check_number(self,string):
        point = 0
        isvalid = True
        for c in string:
            code = ord(c)
            if (code < 48 and code != 46) or code > 57:
                isvalid = False
                break
            elif code == 46:
                point += 1
        if point > 1: isvalid = False
        return isvalid

    def __del__(self):
        self.cur.close()
        self.db.close()


